<?php
namespace Wap\Model;

use Think\Model;

class EnrollRecordModel extends Model
{
    protected $tableName = "enroll_record";
	protected $patchValidate = true;
	protected $_validate = array(
		array( "eid", "require", "没有查到相关活动信息", self::MUST_VALIDATE, '', self::MODEL_BOTH ),//上传者ID;在新增字段的时候进行验证
		array( "token", "require", "", self::MUST_VALIDATE, "", self::MODEL_BOTH ),//指导教师的电话号码，如果填写了就进行验证
		array( "username", "require", "用户名不能为空", self::MUST_VALIDATE, "", self::MODEL_BOTH ),
		array( "telephone", "/(^(13|14|15|18|17)\d{9}$)/", "手机号码格式不正确", self::MUST_VALIDATE, "regex", self::MODEL_BOTH ),//微信环境内只能上传jpg
		array( "telephone", "uniquePhone", "手机号码已经注册过了", self::MUST_VALIDATE, "callback", self::MODEL_BOTH ),//微信环境内只能上传jpg
		//array( "company", "require", "没有公司名称", self::MUST_VALIDATE, "", self::MODEL_BOTH )
	);

    public function registe( $datas )
    {
        //$result = array( "validated" => "", "data" => "" );
        $validated = $this->create( $datas );
        if( !$validated ){
            $result['validated'] = $this->getError();
            return $result;
        }else{
            $result['validated'] = 1;
        }

        $saved = $this->add( $datas );
        if( !$saved ){
            $result['saved'] = 0;
        }else{
            $result['saved'] = 1;
            $result['data'] = $this->where( array( "id" => $saved ) )->find();
        }

        return $result;
    }

    protected function uniquePhone( $telephone )
    {
        //$eid = I( "eid" );
        $condition['eid'] = I( "post.enroll", -1, "" );
        $condition['telephone'] = $telephone;
        $exist = $this->where( $condition )->find();
        if( count( $exist ) ){
             return false;
        }else{
            return true;
        }
    }
}
